﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using DevExpress.Skins;
using DevExpress.Utils.Helpers;

namespace XCI.Windows
{
    public class DesignTimeTools
    {
        public static bool IsDesignMode
        {
            get
            {
                Process process = Process.GetCurrentProcess();
                return (process != null) && IsVSProcess(process.ProcessName);
            }
        }
        public static void EnsureBonusSkins()
        {
            SkinManager.EnableFormSkins();
            try
            {
                Assembly.Load(AssemblyInfo.SRAssemblyBonusSkinsFull);
                ReflectionHelper.InvokeStaticMethod(AssemblyInfo.SRAssemblyBonusSkins, "DevExpress.UserSkins.BonusSkins", "Register");
            }
            catch
            {
            }
        }
        public static Assembly LoadAssembly(string assemblyName, bool checkDTClones)
        {
            if (!checkDTClones) return DoLoadDefaultAssembly(assemblyName);
            Assembly loadedAsm = GetLoadedAssembly(assemblyName);
            if (loadedAsm != null) return loadedAsm;
            return DoLoadDefaultAssembly(assemblyName);
        }
        static Assembly DoLoadDefaultAssembly(string assemblyName)
        {
            return Assembly.Load(assemblyName);
        }
        static Assembly GetLoadedAssembly(string assemblyName)
        {
            List<Assembly> asms = GetLoadedAssemblies(assemblyName);
            return asms != null ? asms.FirstOrDefault() : null;
        }
        static List<Assembly> GetLoadedAssemblies(string assemblyName)
        {
            List<Assembly> asms = null;
            try
            {
                asms = GetAssemblyClonesCore(assemblyName);
                if (asms != null && asms.Count > 1)
                {
                    asms.Sort(new AssemblyComparer());
                }
            }
            catch { }
            return asms;
        }
        static List<Assembly> GetAssemblyClonesCore(string assemblyName)
        {
            List<Assembly> asms = new List<Assembly>();
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                AssemblyName asmName = asm.GetName();
                if (asmName != null && string.Equals(asmName.Name, assemblyName, StringComparison.OrdinalIgnoreCase)) asms.Add(asm);
            }
            return asms.Count > 0 ? asms : null;
        }
        class AssemblyComparer : IComparer<Assembly>
        {
            public int Compare(Assembly x, Assembly y)
            {
                DateTime xts = GetTimestamp(x), yts = GetTimestamp(y);
                if (xts.Equals(yts)) return 0;
                return xts < yts ? 1 : -1;
            }
            protected DateTime GetTimestamp(Assembly asm)
            {
                return File.GetCreationTime(asm.Location);
            }
        }
        internal static bool IsVSProcess(string processName)
        {
            return Array.Exists(Names, delegate(string name)
            {
                return string.Equals(processName, name, StringComparison.OrdinalIgnoreCase);
            });
        }
        static string[] namesCore = null;
        static string[] Names
        {
            get
            {
                if (namesCore == null) namesCore = CreateListCore();
                return namesCore;
            }
        }
        static string[] CreateListCore()
        {
            return new string[] { "devenv", "VCSExpress", "VBExpress", "WDExpress" };
        } 
    }
}